from common import Requestutil
from common import host_port, write_yaml


class LogInApi:
    # 登录
    def login(self, user, password):
        '''
        登录
        :param user:用户名
        :param password:密码
        :return:
        '''
        url = f'https://{host_port}/sys/login'
        header = {'Content-Type': 'application/json'}
        payload = {
            'username': user, 'password': password
        }
        res = Requestutil.send_all_request(method='post', url=url, headers=header, json=payload, verify=False)
        # 解析数据
        json = res.json()
        # 获取code
        code_ = json['code']
        if code_ == 0:
            # 获取token
            token_ = json['token']
            # 保存到yaml，本次测试用例会话持久化数据
            write_yaml({"token": token_})
        return code_
